﻿/**
 *	html/pem_data_user.js
 *
 *	(C) 2014 Yuanjj
 *
 * 	history:
 * 		2014-07-30		create the file
 */

/**
 *	initialize user data
 */
var g_user;
var g_current_user			= "";
var g_curent_privilege 		= 999;
function init_user()
{
    g_current_user = "";

    Ext.define
    ("model_user_info", {
         extend:"Ext.data.Model",
         fields:[
             {name:"account",		type:"string"},
             {name:"password",		type:"string"},
             {name:"privilege",		type:"int"},			// administrator <= 100, other > 100
             {name:"status",		type:"int"}				// 1:"login", 0:"logout" or -1:"unused"
         ]
    });

    g_user = new Ext.data.ArrayStore
    ({
         autoLoad:true,
         model:"model_user_info",
         data:[["administrator","888888"]]
    });
}

/**
 *	check the user if it has administrator right
 *
 *	return
 *		true		has right
 *		false		has no right
 */
function has_administrator_right()
{
    return (g_curent_privilege < 100);
}

/**
 *	load all user form data base
 */
function load_user()
{
    qt_html.load_user();
}

/**
 *	user_login
 *
 *	@account		[in] user account
 *	@passwd			[in] pass word
 */
function user_login( account, passwd )
{
    qt_html.user_login( account, passwd );
}

/**
 *	user login callback
 *
 *	@account		[in] user account
 *	@privilege		[in] user privilege
 *	@status			[in] user status
 */
function callback_user_login( account, privilege, status )
{
    if ( 1 === status )
    {
        g_current_user			= account;
        g_curent_privilege		= privilege;
        g_panel_login.hide();
        g_panel_main.panel_monitor.show();
    }
    else
    {
        if ( undefined !== g_error[status] )
            alert( "登录失败 [" + g_error[status] + "]" );
        else
            alert( "登录失败 [未知错误 error = " + status + "]" );
    }
}
